﻿using System;
using System.Management;
using System.Diagnostics;

namespace HyperV
{
    public partial class VMHelper
    {
        public void CreateDifferencingVirtualHardDisk(string path, string parentPath)
        {
            CreateDifferencingVirtualHardDisk(path, parentPath, Scope);
        }


        static void CreateDifferencingVirtualHardDisk(string path, string parentPath, ManagementScope scope)
        {
            ManagementObject imageService = Utility.GetServiceObject(scope, "Msvm_ImageManagementService");

            ManagementBaseObject inParams = imageService.GetMethodParameters("CreateDifferencingVirtualHardDisk");
            inParams["ParentPath"] = parentPath;
            inParams["Path"] = path;
            ManagementBaseObject outParams = imageService.InvokeMethod("CreateDifferencingVirtualHardDisk", inParams, null);
            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
            {
                if (Utility.JobCompleted(outParams, scope))
                {
                    Debug.WriteLine("{0} was created successfully.", inParams["Path"]);
                }
                else
                {
                    Debug.WriteLine("Unable to create {0}", inParams["Path"]);
                }
            }
            else Debug.WriteLine("Unable to create: " +  inParams["Path"] + " error: " + outParams["ReturnValue"]);

            inParams.Dispose();
            outParams.Dispose();
            imageService.Dispose();
        }
    }
}


